package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 根据充值订单编号查询充值记录
     *
     * @param orderNo
     * @return
     */
    @Override
    public RechargeInfo getRechargeInfo(String orderNo) {
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        return rechargeInfoMapper.selectOne(queryWrapper);
    }


    /**
     * 账户余额充值
     *
     * @param rechargeInfoVo
     * @return 充值记录中订单编号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        //1.获取当前登录用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.构建充值记录对象 封装相关属性
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setUserId(userId);
        String orderNo = "CZ" + DateUtil.today().replaceAll("-", "") + IdUtil.getSnowflakeNextId();
        rechargeInfo.setOrderNo(orderNo);
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        //3.保存充值记录
        rechargeInfoMapper.insert(rechargeInfo);
        //4.TODO 发送延迟消息延迟关闭充值记录
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return map;
    }


    /**
     * 充值支付成功，修改充值状态及变更余额
     *
     * @param orderNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rechargePaySuccess(String orderNo) {
        //1.根据订单编号查询充值记录状态 如果已支付 则直接返回
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(queryWrapper);
        if (rechargeInfo != null && SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())) {
            return;
        }
        //2.修改充值记录状态：已支付
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
        rechargeInfoMapper.updateById(rechargeInfo);

        //3.更新账户余额：总金额、可用余额、收入金额
        userAccountService.add(rechargeInfo.getUserId(), rechargeInfo.getRechargeAmount());

        //4.新增账户变动日志
        userAccountService.saveUserAccountDetail(rechargeInfo.getUserId(), "充值", SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, rechargeInfo.getRechargeAmount(), rechargeInfo.getOrderNo());
    }
}
